using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Shrapnel
{
    public partial class View
    {
        /// <summary>
        /// The print document used to print the workspace view.
        /// </summary>
        private PrintDocument printDocument = new PrintDocument();

        /// <summary>
        /// The settings of the printer.
        /// </summary>
        private PrinterSettings printSettings = new PrinterSettings();

        /// <summary>
        /// The print settings for the pages.
        /// </summary>
        private PageSettings printPageSettings = new PageSettings();

        /// <summary>
        /// Called when the printing settings should be initialized.
        /// </summary>
        private void OnInitializePrinting()
        {
            printDocument.DefaultPageSettings = printPageSettings;
            printDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
        }

        /// <summary>
        /// Called when the actual printing should take place.
        /// </summary>
        /// <param name="sender">The object that called this function.</param>
        /// <param name="e">The printing event arguments.</param>
        private void OnPrintPage(object sender, PrintPageEventArgs e)
        {
            String textToPrint = "TODO: Add printing support";
            Font printFont = new Font("Courier New", 12);
            int leftMargin = e.MarginBounds.Left;
            int topMargin = e.MarginBounds.Top;
            e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin);
        }

        /// <summary>
        /// Called when this view should be printed.
        /// </summary>
        public void Print()
        {
            printDocument.Print();
        }

        /// <summary>
        /// Shows the print dialog.
        /// </summary>
        public void ShowPrintDialog()
        {
            PrintDialog dialog = new PrintDialog();

            dialog.Document = printDocument;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Print();
            }
        }

        /// <summary>
        /// Shows the print page setup dialog.
        /// </summary>
        public void ShowPrintPageSetupDialog()
        {
            PageSetupDialog pageSetupDialog = new PageSetupDialog();
            pageSetupDialog.PageSettings = printPageSettings;
            pageSetupDialog.PrinterSettings = printSettings;
            pageSetupDialog.AllowOrientation = true;
            pageSetupDialog.AllowMargins = true;
            pageSetupDialog.ShowDialog();
        }

        /// <summary>
        /// Shows the print preview dialog.
        /// </summary>
        public void ShowPrintPreviewDialog()
        {
            PrintPreviewDialog dialog = new PrintPreviewDialog();
            dialog.Document = printDocument;
            dialog.ShowDialog();
        }
    }
}
